/*
 * 文 件    名:  BrowsingHistoryService.java
 * 创建日期:  2012-10-22上午10:54:49
 * 版          权:  Epro Technologies Co., Ltd. Copyright 2011-2012,  All rights reserved
 * 作          者:  tj072
 */
package com.weipu.common.facade.content.api;

import java.util.ArrayList;

import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.weipu.common.facade.content.BaseService;
import com.weipu.common.facade.content.ContentURI;
import com.weipu.common.facade.content.colum.BrowsingHistoryColumn;
import com.weipu.common.facade.model.OrderProductModel;

/** 
 * @Description 浏览历史管理
 * @version [版本号, 2012-10-22]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 * 
 * @author tj072
 */
public class BrowsingHistoryService extends BaseService
{
    
    /**
     * 封装cursor 为java bean
     * @param cursor
     * @return [参数说明]
     * @return ArrayList<TemplateModel> [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    private ArrayList<OrderProductModel> getListByCursor(Cursor cursor)
    {
        if (isNullOrEmpty(cursor))
        {
            close(cursor);
            return null;
        }
        ArrayList<OrderProductModel> modelList = new ArrayList<OrderProductModel>(cursor.getCount());
        int[] idx =
            {cursor.getColumnIndex(BrowsingHistoryColumn._ID), cursor.getColumnIndex(BrowsingHistoryColumn.SKU),
                cursor.getColumnIndex(BrowsingHistoryColumn.SHORTHEADLINE1),
                cursor.getColumnIndex(BrowsingHistoryColumn.SMALLPICTUREURL),
                cursor.getColumnIndex(BrowsingHistoryColumn.PRICE),
                cursor.getColumnIndex(BrowsingHistoryColumn.OLDPRICE),
                cursor.getColumnIndex(BrowsingHistoryColumn.REVIEWCOUNT),
                cursor.getColumnIndex(BrowsingHistoryColumn.PRODUCTRATING),cursor.getColumnIndex(BrowsingHistoryColumn.MEDIUMPICTUREURL),};
        
        do
        {
            OrderProductModel model = new OrderProductModel();
            if (-1 != idx[0])
            {
                model.setId(cursor.getInt(idx[0]));
            }
            
            if (-1 != idx[1])
            {
                model.setSku(cursor.getInt(idx[1]));
            }
            
            if (-1 != idx[2])
            {
                model.setShortHeadLine1(cursor.getString(idx[2]));
            }
            if (-1 != idx[3])
            {
                model.setSmallPictureURL(cursor.getString(idx[3]));
            }
            if (-1 != idx[4])
            {
                model.setPrice(cursor.getFloat(idx[4]));
            }
            if (-1 != idx[5])
            {
                model.setlistprice(cursor.getFloat(idx[5]));
            }
            if (-1 != idx[6])
            {
                model.setReviewCount(cursor.getInt(idx[6]));
            }
            if (-1 != idx[7])
            {
                model.setProductRating(cursor.getFloat(idx[7]));
            }
            if (-1 != idx[8])
            {
                model.setMediumPictureURL(cursor.getString(idx[8]));
            }
            modelList.add(model);
        } while (cursor.moveToNext());
        close(cursor);
        return modelList;
    }
    
    /**
     * 获取指定类型的模板列表
     * @param type 日报/周报
     * {@link ContentConstant.Common#CONTENT_TYPE_DAILY}
     * {@link ContentConstant.Common#CONTENT_TYPE_WEEKLY}
     * @return [参数说明]
     * @return ArrayList<TemplateModel> [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public ArrayList<OrderProductModel> getListByType(Context context, String type)
    {
        if (!check(context, type))
        {
            return null;
        }
        return getListByCursor(context.getContentResolver().query(ContentURI.ContentBrowsingHistory.CONTENT_URI_ALL,
            null,
            null,
            null,
            null));
    }
    
    public ArrayList<OrderProductModel> getBySelection(Context context, String selection, String... args)
    {
        if (!check(context))
        {
            return null;
        }
        return getListByCursor(context.getContentResolver().query(ContentURI.ContentBrowsingHistory.CONTENT_URI_ALL,
            null,
            selection,
            args,
            null));
        
    }
    
    /**
     * 根据id查询指定模板
     * @param context
     * @param id 模板id
     * @return [参数说明]
     * @return ArrayList<TemplateModel> [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public OrderProductModel getById(Context context, long id)
    {
        if (!check(context))
        {
            return null;
        }
        if (id < 0)
        {
            return null;
        }
        ArrayList<OrderProductModel> modelList =
            getListByCursor(context.getContentResolver()
                .query(ContentUris.withAppendedId(ContentURI.ContentBrowsingHistory.CONTENT_URI_BY_ID, id),
                    null,
                    null,
                    null,
                    null));
        if (null == modelList || modelList.isEmpty())
        {
            return null;
        }
        return modelList.get(0);
    }
    
    /**
     * 自定义条件查询并返回游标
     * @param context
     * @param projection
     * @param selection
     * @param args
     * @return [参数说明]
     * @return Cursor [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public Cursor queryBySelection(Context context, String[] projection, String selection, String[] args,
        String sortOrder)
    {
        return context.getContentResolver().query(ContentURI.ContentBrowsingHistory.CONTENT_URI_ALL,
            projection,
            selection,
            args,
            sortOrder);
    }
    
    /**
     * 保存
     * @param context
     * @param model
     * @return [参数说明]
     * @return long [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public long save(Context context, OrderProductModel model)
    {
        if (!check(context, model))
        {
            return -1;
        }
        ContentValues map = model.template3map();
        try
        {
            context.getContentResolver().insert(ContentURI.ContentBrowsingHistory.CONTENT_URI_ALL, map);
        }
        catch (Exception e)
        {
            return -1;
        }
        return map.getAsLong(BrowsingHistoryColumn._ID);
    }
    
    /**
     * 保存
     * @param context
     * @param model
     * @return [参数说明]
     * @return long [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public long update(Context context, OrderProductModel model)
    {
        if (!check(context, model))
        {
            return -1;
        }
        
        if (model.getId() < 0)
        {
            return -1;
        }
        
        int updateRow = -1;
        try
        {
            updateRow =
                context.getContentResolver()
                    .update(ContentUris.withAppendedId(ContentURI.ContentBrowsingHistory.CONTENT_URI_BY_ID,
                        model.getId()),
                        model.template3map(),
                        null,
                        null);
        }
        catch (Exception e)
        {
            //ignore
        }
        return updateRow;
    }
    
    /**
     * 根据id删除指定模板
     * @param context
     * @param id 模板id
     * @return [参数说明]
     * @return ArrayList<TemplateModel> [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public int deleteById(Context context, long id)
    {
        if (!check(context))
        {
            return -1;
        }
        
        if (id < 0)
        {
            return -1;
        }
        int deleteRow = -1;
        
        try
        {
            deleteRow =
                context.getContentResolver()
                    .delete(ContentUris.withAppendedId(ContentURI.ContentBrowsingHistory.CONTENT_URI_BY_ID, id),
                        null,
                        null);
        }
        catch (Exception e)
        {
            //ignore
        }
        return deleteRow;
    }
    
    /**
     * 自定义条件删除
     * @param context
     * @param selection 删除条件
     * @param args 条件参数
     * @return [参数说明]
     * @return int [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public int deleteTemplateBySelection(Context context, String selection, String[] args)
    {
        if (!check(context))
        {
            return -1;
        }
        
        int deleteRow = -1;
        try
        {
            deleteRow =
                context.getContentResolver().delete(ContentURI.ContentBrowsingHistory.CONTENT_URI_ALL, selection, args);
        }
        catch (Exception e)
        {
            //ignore
        }
        return deleteRow;
    }
    
    public ArrayList<OrderProductModel> selectMenusList(Context context, String sortOrder)
    {
        Cursor cursor = this.queryBySelection(context, BrowsingHistoryColumn.RESULT, null, null, sortOrder);
        
        ArrayList<OrderProductModel> menuList = new ArrayList<OrderProductModel>();
        cursor.moveToFirst();
        //       ProductServiceProvider productService = new ProductServiceProviderImpl(
        //                null);
        
        //数据集合不为空
        if (cursor.getCount() > 0)
        {
            do
            {
                OrderProductModel temp = new OrderProductModel();
                temp.setId(cursor.getInt(0));
                temp.setSku(cursor.getInt(1));
                temp.setShortHeadLine1(cursor.getString(2));
                temp.setSmallPictureURL(cursor.getString(3));
                temp.setPrice(cursor.getFloat(4));
                temp.setlistprice(cursor.getFloat(5));
                temp.setReviewCount(cursor.getInt(6));
                temp.setProductRating(cursor.getFloat(7));
                if(-1!=cursor.getColumnIndex(BrowsingHistoryColumn.MEDIUMPICTUREURL)){
                	temp.setMediumPictureURL(cursor.getString(8));
                }
                menuList.add(temp);
                
                //后移
            } while (cursor.moveToNext() && cursor.getPosition() < 50);
        }
        cursor.close();
        return menuList;
    }
    
}
